import {requestSubscribeMessage} from "../../utils/util";

const app = getApp()
Page({
  data: {
    changePwd: {
      text: '修改密码',
      imgUrl: '/assets/images/changePwd.png',
      url: '/pages/person/phonePwd/phonePwd',
      // url: '/pages/person/swtp/swtp',
      iconColor: '#FFB911',
      iconName: 'icon-test',
      type: 'image',
    },
    list: [
      // {
      //   text: '帮助&OA',
      //   url: '/page/navigate/navigate',
      //   iconColor: '#2172ED',
      //   iconName: 'icon-bangzhu'
      // },
      {
        text: '关于',
        url: '/pages/person/about/index',
        iconColor: '#FFB911',
        iconName: 'icon-about',
      },
      {
        text: 'U米管理',
        url: '/pages/person/point/index',
        iconColor: '#f34e0d',
        iconName: 'icon-jifen',
        hideCodes: [4144011078],
      },
      // {
      //   text: '',
      //   url: '/pages/person/approval/index',
      //   iconColor: '#60c985',
      //   iconName: 'icon-shenpi1'
      // },
      {
        text: '帮助',
        imgUrl: '/assets/images/help_icon.png',
        url: '/pages/person/help/help?page=my',
        iconColor: '#f34e0d',
        iconName: 'icon-test',
        type: 'image',
      },
    ],
    userInfo: '',
    schoolConfig: '',
    showCustomersService: false,
    showCustomersServiceAvatar: '',
    showCustomersServiceName: '',
  },
  onLoad() {
    this.handleList()
    // this.GuangDongDaXue();
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
      schoolConfig: wx.getStorageSync('schoolConfig'),
    })
    const userId = this.data.userInfo.userId
    const status = wx.getStorageSync('changePwdStatus')
    if (status && status.isChangePwd) {
      this.setData({
        list: this.data.list.concat([this.data.changePwd]),
      })
    }
    return
    if (userId === '2000011116') {
      this.setData({
        'list[2].text': '学院审批',
      })
    } else if (userId === '88888888') {
      this.setData({
        'list[2].text': '教务审批',
      })
      this.data.list[2].text = '教务审批'
    } else {
      this.setData({
        'list[2].text': '审批记录',
      })
    }
  },

  handleClose() {
    this.setData({
      showCustomersService: false,
    })
  },

  // 数组初始化处理
  handleList() {
    // 获取学校代码
    const schoolCode = wx.getStorageSync('schoolConfig').schoolCode
    // 根据学校代码隐藏对应的菜单栏
    let list = this.data.list.map((item) => {
      if (item.hideCodes) {
        item.hide = item.hideCodes.includes(schoolCode)
        return item
      }
      return item
    })
    // 根据学校代码设置特殊的页面路径
    if (schoolCode === 4144011078) {
      list[0].url = '/pages/person/Guangdong/index'
    }
    // 保存更新数据
    this.setData({
      list,
    })
  },
  /**
   * @see 联系我插件调用回调
   */
  completemessage(obj) {
    // 插件文档  https://work.weixin.qq.com/api/doc/90000/90136/92376
    let { headurl, name, errcode } = obj.detail
    if (errcode === 0 || errcode === -3006) {
      this.setData({
        showCustomersService: true,
        showCustomersServiceName: name,
        showCustomersServiceAvatar: headurl,
      })
    } else {
      let str = ''
      switch (errcode) {
        case -3002:
          str = '获取插件配置信息失败'
          break
        case -3004:
          str = '用户信息授权失败'
          break
        case -3005:
          str = '客服消息发送失败'
          break
        case -3008:
          str = '当前配置没有客服人员'
          break
      }
      wx.showModal({
        title: '提示',
        content: str || '数据异常,请稍后再试',
        confirmText: '确定',
        showCancel: false,
      })
    }
  },
  startmessage(...args) {
    console.log(args, 'bbb')
  },

  goPersonDetails() {
    wx.navigateTo({
      url: '/pages/person/personDetails/index',
    })
  },
  handleView() {
    const tmplIds = app.globalData.tmplIds
    if (tmplIds && tmplIds.length) {
      wx.requestSubscribeMessage({
        tmplIds: tmplIds.slice(0, 3),
        fail: (err) => {
          console.log(tmplIds)
          console.log(err)
        },
        success: (res) => {
          requestSubscribeMessage()
          console.log(tmplIds)
          console.log(res)
        },
        complete: () => {
          this.goPersonDetails()
        },
      })
    } else {
      this.goPersonDetails()
    }
  },
  //跳转到小程序站内页面
  navigateTo(e) {
    const { url } = e.currentTarget.dataset
    wx.navigateTo({
      url,
    })
  },
})
